﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.IO;
using System.Text;
using RouterControl.Db;

/// <summary>
/// Summary description for RouterControl
/// </summary>
[WebService(Namespace = "http://www.fkollmann.de/RouterControl")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class RouterControlWS : System.Web.Services.WebService
{
	private DeviceDomDbConnector _db = new DeviceDomDbConnector();

	public RouterControlWS()
	{

		//Uncomment the following line if using designed components 
		//InitializeComponent(); 
	}

	[WebMethod]
	public void UploadRouterDescription(string routerUniqueId, string xmlBase64)
	{
		if (string.IsNullOrEmpty(routerUniqueId))
			throw new ArgumentException("null or empty", "routerUniqueId");

		if (string.IsNullOrEmpty(xmlBase64))
			throw new ArgumentException("null or empty", "xmlBase64");

        throw new NotSupportedException("feature is currently disabled");
        /* TODO: implement device dom loading support

		// is mime64?
		var xmlUtf8 = Convert.FromBase64String(xmlBase64);

		var xml = Encoding.UTF8.GetString(xmlUtf8);

		// load xml
		var trans = DeviceSchemaDomTransform.CreateFromSchemaXml(xml);

		// add
		_db.SaveDeviceSchema(trans);*/
	}

	[WebMethod]
	public bool HasRouterDescription(string routerUniqueId)
	{
		if (string.IsNullOrEmpty(routerUniqueId))
			throw new ArgumentException("null or empty", "routerUniqueId");

		return _db.HasDeviceSchema(routerUniqueId);
	}

	[WebMethod]
	public bool HasRouterDescriptionEx(string routerUniqueId, int versionId)
	{
		if (string.IsNullOrEmpty(routerUniqueId))
			throw new ArgumentException("null or empty", "routerUniqueId");

		return _db.HasDeviceSchema(routerUniqueId, versionId);
	}

	[WebMethod]
	public string GetMaxSpecDeviceDom(string rootDeviceTypeId)
	{
		if (string.IsNullOrEmpty(rootDeviceTypeId))
			throw new ArgumentException("null or empty", "rootDeviceTypeId");

        throw new NotSupportedException("feature is currently disabled");
        /* TODO: implement device dom loading support

		// retrieve
		var schemaId = MaxSpecDom.GetSchemaId(rootDeviceTypeId);

		var dom = _db.LoadDeviceDom(schemaId);

		// handle if not found
		if (dom == null)
			return string.Empty;

		// serialize
		return Convert.ToBase64String(Encoding.UTF8.GetBytes(dom.ToXml()));*/
	}

	[WebMethod]
	public string GetNewerVersion(int versionId)
	{
        var latest = _db.GetLatesRelease(true, false);

		_db.LogUpdateCheck(
            Guid.Empty,
            versionId,
            latest.ReleaseId,
            latest.VersionStr
            );

		if (versionId < latest.ReleaseId)
			return latest.VersionStr;

		return string.Empty;
	}

    [WebMethod]
    public DeviceDomDbConnector.ReleaseInfo GetNewerVersion2(int versionId)
    {
        var latest = _db.GetLatesRelease(true, false);

        _db.LogUpdateCheck(
            Guid.Empty,
            versionId,
            latest.ReleaseId,
            latest.VersionStr
            );

        return latest;
    }

    [WebMethod]
    public DeviceDomDbConnector.ReleaseInfo GetNewerVersion3(string instanceId, int versionId, bool includeBetaRelease)
    {
        var latest = _db.GetLatesRelease(true, includeBetaRelease);

        _db.LogUpdateCheck(
            new Guid(instanceId),
            versionId,
            latest.ReleaseId,
            latest.VersionStr
            );

        return latest;
    }

    public class ErrorInfo
    {
        public string InstanceGuid;     // used to group error messages by runtime Router Control instance
        public int VersionId = -1;
        public DateTime TimeStampUtc;
        public int Count = 1;

        public string ErrorType;
        public string Message;
        public string ClassName;
        public string MethodName;
        public string StackTrace;
        public string ErrorHash;

        public ErrorInfo Inner;

        public DeviceDomDbConnector.ErrorInfo ToDatabase()
        {
            return new DeviceDomDbConnector.ErrorInfo()
            {
                InstanceId = new Guid(this.InstanceGuid),
                ReleaseId = this.VersionId,
                TimeStampUtc = this.TimeStampUtc,
                Count = this.Count,

                ErrorType = this.ErrorType,
                Message = this.Message,
                ClassName = this.ClassName,
                MethodName = this.MethodName,
                StackTrace = this.StackTrace,
                ErrorHash = this.ErrorHash,

                Inner = (this.Inner == null ? null : this.Inner.ToDatabase())
            };
        }
    }

    [WebMethod]
    public void ReportError(ErrorInfo error)
    {
        _db.AddErrorReport(
            error.ToDatabase()
            );
    }

}

